<script setup>
import Sidebar from './components/Sidebar.vue';
import local from './components/local.vue'
import { defineAsyncComponent, ref, onMounted, watch } from 'vue';
import { useIndexStore } from './pinia';
import { ElMessage } from "element-plus"
import { useRoute } from 'vue-router'

let route = useRoute()
let store = useIndexStore()
let username = localStorage.getItem('username')
//根据权限转动态路由
if (username) {
  store.roleChange(username)
}

let sidebar = defineAsyncComponent(() => {
  return new Promise((res, rej) => {
    res(Sidebar)
  })
})
let locabob = ref(true)

setTimeout(() => {
  locabob.value = false
}, 300)

//工作日设置提示
let calender = ref(new Date())
let years = ref(calender.value.getFullYear())
let mon = ref(calender.value.getMonth() + 1)
let day = ref(calender.value.getDate())
let newbeizhu = ref('')

let num = ref(1)
watch(route, (newV) => {
  if (newV.name == "login") {
    let username = localStorage.getItem('username')
    if (username) {
      return;
    }
    num.value = 0
    return;
  } else if (newV.name == "index") {
    if (num.value == 0) {
      let usernameB = localStorage.getItem('usernameB')
      let username = localStorage.getItem('username')
      if (usernameB != username) {
        return
      }
      newbeizhu.value = JSON.parse(localStorage.getItem("beizhu"))
      newbeizhu.value.forEach((item, index) => {
        if (item.year.substr(0, 4) == years.value) {
          item.month.forEach((itemm, indexm) => {
            if (itemm == mon.value) {
              item.day[indexm].forEach((itemd, indexd) => {
                //当日
                if (day.value == itemd) {
                  if (item.content[indexm][indexd] == "") {
                    return;
                  }
                  ElMessage.success("今天：" + item.content[indexm][indexd])
                }
                //后二天
                if (day.value < itemd && itemd - day.value < 3) {
                  if (item.content[indexm][indexd] == "") {
                    return;
                  }
                  ElMessage.warning(itemd + "号" + item.content[indexm][indexd])
                }
                //前一天
                if (day.value > itemd && day.value - itemd < 2) {
                  if (item.content[indexm][indexd] == "") {
                    return;
                  }
                  if (item.content[indexm][indexd].indexOf("已读") == -1) {
                    ElMessage.info(itemd + "号" + item.content[indexm][indexd])
                    item.content[indexm][indexd] = item.content[indexm][indexd].concat("-已读")
                  }

                }
              })
            }
          })
        }
      });
      localStorage.setItem("beizhu", JSON.stringify(newbeizhu.value))
      num.value = 1
    }
  }

})
</script>

<template>
  <transition name="fade">
    <local v-show="locabob" />
  </transition>

  <sidebar />

  <router-view v-slot="{ Component }">
    <transition name="fade">
      <component :is="Component" />
    </transition>
  </router-view>
</template>

<style scoped lang="less">
.fade-enter-active {
  transition: opacity .2s ease-in-out;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>